/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.model;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Berto_Romero
 */
public class MyEntityManagerFactory {
    
    private static MyEntityManagerFactory myEntityManager = null;
    private final EntityManagerFactory entityManagerFactory;
    private static final PersistenceProperties PERSISTENCE_PROPERTIES = PersistenceProperties.getInstance();

    private MyEntityManagerFactory() {
        Map<String, String> persistenceMap = new HashMap<>();
        persistenceMap.put("javax.persistence.jdbc.url", "jdbc:mysql://" + PERSISTENCE_PROPERTIES.getUrl() + ":" + 
                PERSISTENCE_PROPERTIES.getPort() + "/dreamwar_sif?zeroDateTimeBehavior=convertToNull");
        persistenceMap.put("javax.persistence.jdbc.user", PERSISTENCE_PROPERTIES.getUser());
        persistenceMap.put("javax.persistence.jdbc.password", PERSISTENCE_PROPERTIES.getPassword());
        entityManagerFactory = Persistence.createEntityManagerFactory("dreamwar_sif?zeroDateTimeBehavior=convertToNullPU", persistenceMap);
    }
    
    public static MyEntityManagerFactory getInstance() {
        if (PERSISTENCE_PROPERTIES.isReady() && myEntityManager == null) {
            myEntityManager = new MyEntityManagerFactory();
        }
        return myEntityManager;
    }

    public EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    public EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}
